package com.yuandian.ops.workbench.controller;


import com.alibaba.csp.sentinel.util.StringUtil;
import com.yuandian.bpm.admin.api.entity.SysDictItem;
import com.yuandian.bpm.admin.api.feign.RemoteDictService;
import com.yuandian.bpm.admin.api.feign.RemoteParamService;
import com.yuandian.bpm.common.core.constant.SecurityConstants;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.constant.ConfigKey;
import com.yuandian.constant.DictType;
import com.yuandian.ops.workbench.po.SystemHealthParamsYwgzt;
import com.yuandian.ops.workbench.service.ISystemYwgztService;
import com.yuandian.ops.workbench.vo.SystemDomainOptionInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>系统域controller</p>
 *
 * @author LinShiHe
 * @Date 2021/07/02
 */
@Slf4j
@RestController
@RequestMapping("/ywgzt/system")
@Api(tags = "首页系统域-Controller")
public class SystemYwgztController {

	@Autowired
	private RemoteDictService remoteDictService;

	@Autowired
	private ISystemYwgztService systemYwgztService;

	@Autowired
	private RemoteParamService remoteParamService;


	/**
	 * 根据参数键名查询参数值
	 *
	 * @return
	 */
	@GetMapping(value = "/configKey")
	public R<Object> getConfigKey() {
		Map<String, String> resMap = new HashMap<>();
		// 定义首页大屏 '刷新时间'、 '是否刷新'
		String refreshTime = "", refreshFlag = "";

		try {
			long sTime = System.currentTimeMillis();
			refreshTime = remoteParamService.getByKeyNoInner(ConfigKey.YWGZT_HOME_REFRESH_TIME).getData();
			refreshFlag = remoteParamService.getByKeyNoInner(ConfigKey.YWGZT_HOME_REFRESH_FLAG).getData();

			log.info("[运维工作台]-[远程调用-后台配置服务]-[获取公共参数配置]-耗时: {} MS", System.currentTimeMillis() - sTime);

		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-后台配置服务]-[获取公共参数配置]-报错: {} ", e.getMessage());
		}

		resMap.put("refreshTime", StringUtil.isEmpty(refreshTime) ? "60" : refreshTime);
		resMap.put("refreshFlag", StringUtil.isEmpty(refreshFlag) ? "true" : refreshFlag);
		return R.ok(resMap);
	}
/*

	*//**
	 * 运维工作台-更新首页刷新时间
	 * @return
	 *//*
	@PostMapping("/editDevOpsHomePageRefreshTime")
	public Result editDevOpsHomePageRefreshTime(@RequestBody SystemRefreshData params) {

		try{
			String refreshTimeName = "ywgzt.home.cockpit.refreshTime";
			String refreshFlagName = "ywgzt.home.refresh.flag";
			// 1. 根据参数键名查询对应的参数对象
			ConfigPageParams configPageParams = new ConfigPageParams();
			configPageParams.setConfigKey(refreshTimeName);
			configPageParams.setPageNum(1);
			configPageParams.setPageSize(1);
			PageDataInfo list = configService.selectConfigList(configPageParams);
			if (list.getData().size() == 0) {
				return Result.setFail("参数键名不存在");
			}
			ConfigInfo configInfo = (ConfigInfo) list.getData().get(0);
			configInfo.setConfigValue(params.getRefreshTime());
			configService.updateConfig(configInfo);

			configPageParams.setConfigKey(refreshFlagName);
			configPageParams.setPageNum(1);
			configPageParams.setPageSize(1);
			PageDataInfo list2 = configService.selectConfigList(configPageParams);
			if (list.getData().size() == 0) {
				return Result.setFail("参数键名不存在");
			}
			ConfigInfo configInfo2 = (ConfigInfo) list2.getData().get(0);
			configInfo2.setConfigValue(params.getRefreshFlag());
			configService.updateConfig(configInfo2);
		} catch (Exception e) {
			log.error("[运维工作台]-设置首页刷新配置失败!");
			Result.setFail("设置首页刷新配置失败");
		}
		// 2. 修改参数
		return Result.setSuccess();
	}*/

	/**
	 * 系统域列表
	 * @return 返回对象
	 */
	@ApiOperation(value = "系统域列表", notes = "系统域列表")
    @GetMapping("/getSystemDomainOption")
    public R<Object> getSystemDomainOption() {
		List<SysDictItem> dictList = null;
		R<List<SysDictItem>> dictByType = null;
		try {
			long sTime = System.currentTimeMillis();
			dictByType = remoteDictService.getDictByType(DictType.LOG_TYPE);
			log.info("[运维工作台]-[远程调用-后台配置服务]-[获取字典数据]-耗时: {} MS", System.currentTimeMillis() - sTime);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("[运维工作台]-[远程调用-后台配置服务]-[获取字典数据]-报错: {} ", e.getMessage());
		}
		if (!Objects.isNull(dictByType)) { dictList = dictByType.getData(); }
		List<SystemDomainOptionInfoVo> systemDomainList = new ArrayList<>();
		if (!CollectionUtils.isEmpty(dictList)) {
			for (SysDictItem item : dictList) {
				SystemDomainOptionInfoVo vo = new SystemDomainOptionInfoVo();
				vo.setSystemDomainId(item.getId());
				vo.setSystemDomainCode(item.getItemValue());
				vo.setSystemDomainName(item.getLabel());
				systemDomainList.add(vo);
			}
		}
		return R.ok(systemDomainList);
    }

	/**
	 * 根据系统分类与系统名称查询系统健康度列表
	 * @param params 请求对象
	 * @return 返回对象
	 */
	@ApiOperation(value = "根据系统分类与系统名称查询系统健康度列表", notes = "根据系统分类与系统名称查询系统健康度列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", required = true, dataType = "String")
	})
	@PostMapping("/getSystemHealthList")
	public R<Object> getSystemHealthList(@RequestBody SystemHealthParamsYwgzt params){

		return systemYwgztService.getSystemHealthList(params);
	}

}
